package redis

import (
	"context"
	"fmt"
	"rpc/pb/cart"
	"strconv"
)

func CartAdd(userId int64, goodsId int64, cart map[string]interface{}) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.HMSet(context.Background(), key, cart).Err()
}
func GetExists(userId int64, goodsId int64) bool {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.Exists(context.Background(), key).Val() == 1
}
func CartUpdate(userId int64, goodsId int64, num int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.HIncrBy(context.Background(), key, "num", num).Err()
}
func CartDel(userId int64, goodsId int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.Del(context.Background(), key).Err()
}
func CartList(userId int64, goodsId int64) (cartList []*cart.CartList, err error) {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	goodsMap := Rdb.HGetAll(context.Background(), key).Val()

	goodsPrice, _ := strconv.ParseFloat(goodsMap["goods_price"], 64)
	goodsStatus, _ := strconv.Atoi(goodsMap["goodsStatus"])
	checked, _ := strconv.Atoi(goodsMap["checked"])
	userid, _ := strconv.Atoi(goodsMap["userid"])
	goods_id, _ := strconv.Atoi(goodsMap["goods_id"])
	num, _ := strconv.Atoi(goodsMap["num"])

	cartList = append(cartList, &cart.CartList{
		GoodsTitle:  goodsMap["goods_title"],
		GoodsPrice:  float32(goodsPrice),
		GoodsCover:  goodsMap["goods_cover"],
		GoodsStatus: int64(goodsStatus),
		Checked:     int64(checked),
		UserId:      int64(userid),
		GoodsId:     int64(goods_id),
		Num:         int64(num),
	})
	return
}
func CartChecked(userId int64, checked int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	Rdb.HSet(context.Background(), key, "checked", checked)
}
func CartCount(userId int64) (count int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		goodsMap := Rdb.HGetAll(context.Background(), v).Val()

		num, _ := strconv.Atoi(goodsMap["num"])
		count += int64(num)
	}
	return
}
func CartAmount(userId int64) (amount float64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		goodsMap := Rdb.HGetAll(context.Background(), v).Val()

		num, _ := strconv.Atoi(goodsMap["num"])
		price, _ := strconv.ParseFloat(goodsMap["goods_price"], 64)

		amount += price * float64(num)
	}
	return
}
func CartItem(userId int64) (cartItem []*cart.CartItem, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		goodsMap := Rdb.HGetAll(context.Background(), v).Val()

		goodsPrice, _ := strconv.ParseFloat(goodsMap["goods_price"], 64)
		goodsStatus, _ := strconv.Atoi(goodsMap["goodsStatus"])
		checked, _ := strconv.Atoi(goodsMap["checked"])
		userid, _ := strconv.Atoi(goodsMap["userid"])
		goods_id, _ := strconv.Atoi(goodsMap["goods_id"])
		num, _ := strconv.Atoi(goodsMap["num"])

		cartItem = append(cartItem, &cart.CartItem{
			GoodsTitle:  goodsMap["goods_title"],
			GoodsPrice:  float32(goodsPrice),
			GoodsCover:  goodsMap["goods_cover"],
			GoodsStatus: int64(goodsStatus),
			Checked:     int64(checked),
			UserId:      int64(userid),
			GoodsId:     int64(goods_id),
			Num:         int64(num),
		})
	}

	return
}

func CartClear(userId int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		fmt.Println(v)
		Rdb.Del(context.Background(), v)
	}
}
func CartStatus(userId int64, goodsId int64, checked int64) {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	Rdb.HSet(context.Background(), key, "checked", checked)
}
